package org.sercho.masp.notification.decision;

import org.sercho.masp.notification.NotificationSystem;
import org.sercho.masp.notification.settings.NotificationSettings;

/**
 * This decider calculates the actual presentation-mode for a notification,
 * depending on the default- and user-urgency.
 * 
 * @author Benny
 * 
 */
public class PresentationDecider {
	/**
	 * An array holding the presentation-modes. The presentation-mode for an
	 * urgency u is found at the index u-1.
	 */
	private int[] urgencyPresentationModeMap = { 1, 2, 3, 4, 5 };

	/**
	 * Constructor.
	 */
	protected PresentationDecider() {
	}

	/**
	 * Calculates the presentation-mode for the notification of a message of the
	 * category with the given ID for the given user on the given time of
	 * notification. When the message is not registered, no time of notification
	 * should be passed. Rather, an urgency should be passed. Then this urgency
	 * will be returned. When the message is registered, the stored urgency of
	 * the user will be returned. If no urgency for this message at this time is
	 * stored for the user, the stored default-urgency will be returned.
	 * 
	 * @param userId
	 *            the ID of the user, the message is meant for
	 * @param messageId
	 *            the unique ID of the message-category
	 * @param timeOfNotificationId
	 *            the ID of the current time of notification
	 * @param defaultUrgency
	 *            an urgency to be returned, if no time of notification is
	 *            passed
	 * @return the presentation-mode for the notification of the message for the
	 *         specific user
	 */
	protected int getPresentationMode(String userId, String messageId,
			String timeOfNotificationId, int defaultUrgency) {
		if (timeOfNotificationId != null && !timeOfNotificationId.isEmpty()) {
			defaultUrgency = getDefaultUrgency(messageId, timeOfNotificationId);
		}
		int userUrgency = getUserUrgency(userId, messageId,
				timeOfNotificationId);
		int usedUrgency = (userUrgency != 0) ? userUrgency
				: (defaultUrgency != 0) ? defaultUrgency : 3;
		return this.urgencyPresentationModeMap[usedUrgency - 1];
	}

	/**
	 * Retrieves the user-urgency for the given message-category and time of
	 * notification.
	 * 
	 * @param userId
	 *            the ID of the user, whose urgency shall be retrieved
	 * @param messageId
	 *            the unique ID of the message-category
	 * @param timeOfNotificationId
	 *            the ID of the time of notification, to retrieve the urgency
	 *            for
	 * @return the user-urgency
	 */
	private int getUserUrgency(String userId, String messageId,
			String timeOfNotificationId) {
		NotificationSettings settings = NotificationSystem.getInstance()
				.getSettings();
		return settings.getUserUrgency(userId, messageId, timeOfNotificationId);
	}

	/**
	 * Retrieves the default-urgency for the given message-category and time of
	 * notification, which was registered by the message-source.
	 * 
	 * @param messageId
	 *            the unique ID of the message-category
	 * @param timeOfNotificationId
	 *            the ID of the time of notification, to retrieve the urgency
	 *            for
	 * @return the default-urgency
	 */
	private int getDefaultUrgency(String messageId, String timeOfNotificationId) {
		NotificationSettings settings = NotificationSystem.getInstance()
				.getSettings();
		return settings.getDefaultUrgency(messageId, timeOfNotificationId);
	}

}
